package old;

import java.util.*;

public class BasicFeaturesV1_old1 {
  
  public static final int SIZE = 999;
  ArrayList<Task> everything;
  ArrayList<Task> temp; // to store tasks that are viewed
  ArrayList<Task> labelled; // to store tasks which has the required label
  ArrayList<Task> viewList; // to store viewlist arraylist
  /* viewList ArrayList needs to be flushed after showing the information to the user */
  ArrayList<Task> upcomingTask;
  
  public BasicFeaturesV1_old1(){
    everything = new ArrayList<Task>(SIZE); 
    temp = new ArrayList<Task>(SIZE);
    labelled = new ArrayList<Task>(SIZE);
    viewList = new ArrayList<Task>(SIZE);
    upcomingTask = new ArrayList<Task>(SIZE);
  }
  
  public void addTask (Task task) {
    everything.add(task);
    sort(); // call sort method to sort the Task objects according to upcoming
  }
  
  public void removeTask(int index) {
    int size = everything.size();
    Task item = temp.get(index);
    Task taskinlist;
    int i;
    for(i=0; i<size; i++){
      taskinlist = everything.get(i);
      if(item.compareTask(taskinlist)){// can an equals(Task task) method be created in the Task class?
        break;
      }
    }
    everything.remove(i); 
  }
  
  public ArrayList<Task> viewbyDate (int index) { // viewlist by date
    Task taskinlist;
    int year = index%100;
    int month = (index/100)%100;
    int date = ((index/100)/100)%100;
    int size = everything.size();
    
    for (int i=0; i<size; i++) {
      taskinlist = everything.get(i);
      if (taskinlist.getYear() == year && taskinlist.getMonth() == month && taskinlist.getDate() == date)
        viewList.add(taskinlist);
    }
    
    return viewList;
  }
  
  public ArrayList<Task> viewbyDatetoDate (int startDate, int endDate) { // viewlist by start date to end date
    Task taskinlist;
    int year1 = startDate%100;
    int month1 = (startDate/100)%100;
    int date1 = (startDate/10000)%100;
    int year2 = endDate%100;
    int month2 = (endDate/100)%100;
    int date2 = (endDate/10000)%100;
    int size = everything.size();
    
    for (int i=0; i<size; i++) {
      taskinlist = everything.get(i);
      if (taskinlist.getDate() >= date1 && taskinlist.getDate() <= date2 && taskinlist.getMonth() >= month1 && taskinlist.getMonth() <= month2 && taskinlist.getYear() >= year1 && taskinlist.getYear() <= year2)
        viewList.add(taskinlist);
    }
    
    return viewList;
  }
  
  /* not sure if needed
   public void checkLabels (Task task, String label) {
   if (task.hasLabel()) { // add a hasLabel in Task class?
   if (task.getLabel().equals(label))
   labelled.add(task);
   }
   }
   */
  
  public ArrayList<Task> viewbyLabel (String label) { // viewlist by label
    Task taskinlist;
    int size = everything.size();
    
    for (int i=0; i<size; i++) {
      taskinlist = everything.get(i);
      if (taskinlist.getLabel().equals(label))
        labelled.add(taskinlist);
    }
    
    return labelled;
    
  }
  
  public ArrayList<Task> viewbyImportance() { // 1 = important
    Task taskinlist = new Task();
    int size = everything.size();
    
    for (int i=0; i<size; i++) {
      taskinlist = everything.get(i);
      if (taskinlist.hasImportance())
        viewList.add(taskinlist);
    }
    
    return viewList;
    
  }
  
  public ArrayList<Task> viewbyDueDate (int dueDate) {
    Task taskinlist;
    int size = everything.size();
    
    for (int i=0; i<size; i++) {
      taskinlist = everything.get(i);
      if (taskinlist.getDueDate() == dueDate) // Task class has getDueDate()?
        viewList.add(taskinlist);
    }
    
    return viewList;
  }
  
  /*public void sort() {
   Task taskinlist;
   Task taskinlistA;
   Task taskinlistB;
   Task taskintemplist;
   int i=0, j=0, k=0;
   int everything_size = everything.size();
   int temp_size = temp.size();
   
   for (i=0; i<everything_size-1; i++) {
   for (j=1; j<i+2; j++) {
   taskinlistA = everything.get(i);
   taskinlistB = everything.get(j);
   if (taskinlistA.getYear() > taskinlistB.getYear()) {
   if (taskinlistA.getMonth() > taskinlistB.getMonth()) {
   if (taskinlistA.getDate() > taskinlistB.getDate()) {
   for (k=j; k<everything_size-j; k++) {
   taskinlist = everything.get(k);
   temp.add(taskinlist);
   }
   }
   else {
   if (taskinlistA.getDate() == taskinlistB.getDate()) {
   if (taskinlistA.getStartTime() > taskinlistB.getStartTime()) { // if the date/time in previous is larger than the next
   for (k=j; k<everything_size-j; k++) {
   taskinlist = everything.get(k);
   temp.add(taskinlist); // add the behind objects into temp ArrayList
   
   }
   }
   }
   }
   }
   }
   }
   for (i=0; i<temp_size; i++) {
   taskintemplist = temp.get(i);
   if (taskinlistA.getYear() > taskintemplist.getYear()) {
   if (taskinlistA.getMonth() > taskintemplist.getMonth()) {
   if (taskinlistA.getDate() > taskintemplist.getDate()) {
   everything.add(taskintemplist);
   }
   else {
   if (taskinlistA.getDate() == taskintemplist.getDate()) {
   if (taskinlistA.getStartTime() > taskintemplist.getStartTime()) { // if the date/time in previous is larger than the next
   everything.add(taskintemplist);
   else
   everything.add(taskintemplist);
   }
   }
   }
   }
   }
   } // add all the objects in temp ArrayList into everything ArrayList
   }
   }
   }
   } */
  
  public void sort() {
    int a, i, j=0;
    int size = everything.size();
    Task temp1, temp2, temp;
    
    for (a=0; a<size; a++) {
      for (i=1; i<size; i++) {
        j = i - 1;
        temp1 = everything.get(j);
        temp2 = everything.get(i);
        
        if (temp1.getYear() > temp2.getYear()) {
          everything.set(i, temp1);
          everything.set(j, temp2);
        }
        if (temp1.getYear() == temp2.getYear()) {
          if (temp1.getMonth() > temp2.getMonth()) {
            everything.set(i, temp1);
            everything.set(j, temp2);
          }
        }
        
        if (temp1.getYear() == temp2.getYear()) {
          if (temp1.getMonth() == temp2.getMonth()) {
            if (temp1.getDate() > temp2.getDate()) {
              everything.set(i, temp1);
              everything.set(j, temp2);
            }
          }
        }
        
        if (temp1.getYear() == temp2.getYear()) {
          if (temp1.getMonth() == temp2.getMonth()) {
            if (temp1.getDate() == temp2.getDate()) {
              if (temp1.getStartTime() > temp2.getStartTime()) {
                everything.set(i, temp1);
                everything.set(j, temp2);
              }
            }
          }
        }
      }
    }
  }
  
  /*
   public void sort() {
   int i;
   int j = 0;
   int size = everything.size();
   System.out.println("size = "  + size);
   Task temp1;
   Task temp2;
   Task temp;
   for(i=1; i<size; i++){
   
   //System.out.println("loop entered");
   
   j = i - 1;
   temp1 = everything.get(j);
   temp2 = everything.get(i);
   System.out.println("temp2 = " + temp2.getDate() + temp2.getMonth() + temp2.getYear());
   if (temp1.getYear() >= temp2.getYear()) {
   if (temp1.getMonth() > temp2.getMonth()) {
   //if (temp1.getDate() > temp2.getDate()) {
   //if (temp1.getStartTime() > temp2.getStartTime()) {
   System.out.println(" if area entered" );
   
   everything.set(i, temp1);
   everything.set(j, temp2);
   
   }}//}}
   //logic to be settled later
   }
   } 
   
   */
  public ArrayList<Task> retrieveUpcomingTask() { // viewall
    Task taskinlist;
    int size = everything.size();
    
    for (int i=0; i<size; i++) {
      taskinlist = everything.get(i);
      upcomingTask.add(taskinlist);
    }
    
    return upcomingTask;
  }
  
  public ArrayList<Task> retrieveUpcomingTask(int index) { // index is the number of next upcoming tasks
    Task taskinlist;
    int max = everything.size();
    
    for (int i=0; i<max; i++) {
    	if(i < index){
    		taskinlist = everything.get(i);
    		upcomingTask.add(taskinlist);
    	}
    	else break;
    }
    
    return upcomingTask;
  }
  
  public Task specificTask(int index) {
    Task taskinlist = new Task();
    int size = everything.size();
    
    for (int i=0; i<size; i++) {
      taskinlist = everything.get(i);
      if (i==index+1)
        break;
    }
    
    return taskinlist;
  }
  
  public void printTask(Task task){
    System.out.print(task.getDate());
    System.out.print(task.getMonth());
    System.out.print(task.getYear());
    System.out.print(task.getStartTime());
    System.out.print(task.getEndTime());
    System.out.print(task.getDescription());
    System.out.print(task.getLabel());
    System.out.print(task.getVenue());
  }
  
  public void printeverything() {
    int size = everything.size();
    
    for (int i=0; i<size; i++) {
      System.out.print(everything.get(i).getDate());
      System.out.print(" ");
      System.out.print(everything.get(i).getMonth());
      System.out.print(" ");
      System.out.print(everything.get(i).getYear());
      System.out.print(" ");
      System.out.print(everything.get(i).getStartTime());
      System.out.print(" ");
      System.out.print(everything.get(i).getEndTime());
      System.out.print(" ");
      System.out.print(everything.get(i).getDescription());
      System.out.print(" ");
      System.out.print(everything.get(i).getLabel());
      System.out.print(" ");
      System.out.print(everything.get(i).getVenue());
      System.out.print(" ");
    }
  }
}
